package com.hoongs.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.hoongs.v1.vo.AddressVo;
import com.hoongs.v1.entity.*;
import com.hoongs.v1.mapper.AddressMapper;
import com.hoongs.v1.mapper.UserMapper;
import com.hoongs.v1.service.AddressService;
import com.hoongs.v1.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {

    @Resource
    private AddressMapper addressMapper;
    @Resource
    private UserMapper userMapper;


    /**
     * 根据用户查询单个地址信息
     *
     * @param token
     * @return
     */
    @Override
    public Result selectInfoByUser(String id, String token) {
        User user = UserServiceImpl.getUserInfoByToken(token, userMapper);
        if (user == null) return Result.opError("请先登录！");
        Address address = addressMapper.selectById(id);
        if (!address.getUserId().equals(user.getId())) return Result.opError("您无权查看该地址信息！");
        AddressVo addressVo = AddressVo.voFromDao(address);
        if (user.getDefaultAddress().equals(addressVo.getId())) {
            addressVo.setIsDefault("1");
        } else {
            addressVo.setIsDefault("0");
        }
        return Result.success(addressVo);

    }

    /**
     * 根据用户查询默认地址信息
     *
     * @param token
     * @return
     */
    @Override
    public Result selectDefaultByUser(String token) {
        User user = UserServiceImpl.getUserInfoByToken(token, userMapper);
        if (user == null) return Result.opError("请先登录！");
        Address address = addressMapper.selectById(user.getDefaultAddress());
        if (address == null) return Result.error("没有选择地址");
        if (!address.getUserId().equals(user.getId())) return Result.opError("您无权查看该地址信息！");
        AddressVo addressVo = AddressVo.voFromDao(address);
        addressVo.setIsDefault("1");
        return Result.success(addressVo);
    }

    /**
     * 根据用户查询所有购物车数据
     *
     * @param token
     * @return
     */
    @Override
    public Result selectListByUser(String token) {
        User user = UserServiceImpl.getUserInfoByToken(token, userMapper);
        if (user == null) return Result.opError("请先登录！");
        QueryWrapper<Address> wrapper = new QueryWrapper();
        wrapper.eq("user_id", user.getId());
        List<Address> list = addressMapper.selectList(wrapper);
        List<AddressVo> voList = new ArrayList<>();
        for (Address address : list) {
            AddressVo addressVo = AddressVo.voFromDao(address);
            if (user.getDefaultAddress().equals(address.getId())) {
                addressVo.setIsDefault("1");
            } else {
                addressVo.setIsDefault("0");
            }
            voList.add(addressVo);
        }

        return Result.success(voList);
    }

    /**
     * 根据用户id更新地址数据
     *
     * @param addressVO
     * @param token
     * @return
     */
    @Override
    @Transactional
    public Result saveByUser(AddressVo addressVO, String token) {
        User user = UserServiceImpl.getUserInfoByToken(token, userMapper);
        if (user == null) return Result.opError("请先登录！");
        List<Address> list = addressMapper.selectList(new QueryWrapper<Address>().eq("user_id", user.getId()));
        if (list.size() < 5) {
            if ("1".equals(addressVO.getIsDefault())) {
                user.setDefaultAddress(addressVO.getId());
                userMapper.updateById(user);
            }
            if (StringUtils.isNotBlank(addressVO.getId())) {
                if (!user.getId().equals(addressVO.getUserId())) return Result.opError("您无权操作该地址信息！");
                return this.update(AddressVo.voToDao(addressVO));
            } else {
                Address address = AddressVo.voToDao(addressVO);
                address.setUserId(user.getId());
                if (addressMapper.insert(address) == 1) {
                    if(list.size() == 0){
                        user.setDefaultAddress(address.getId());
                    }
                    userMapper.updateById(user);
                    return Result.opSuccess();
                }else {
                    return Result.opError("地址保存失败！");
                }
            }
        } else {
            return Result.opError("每个用户最多保存五条地址信息！");
        }

    }


    @Override
    @Transactional
    public Result removeByUser(String id, String token) {
        User user = UserServiceImpl.getUserInfoByToken(token, userMapper);
        if (user == null) return Result.opError("请先登录！");
        Address address = addressMapper.selectById(id);
        if (address.getUserId().equals(user.getId())) {
            if (addressMapper.deleteById(id) == 1) {
                if (address.getId().equals(user.getDefaultAddress())) {
                    user.setDefaultAddress(null);
                    userMapper.updateById(user);
                }
                return Result.opSuccess();
            } else {
                return Result.opError();
            }
        } else {
            return Result.opError("您无权操作该地址信息！");
        }
    }

    @Override
    public Result add(Address address) {
        if (addressMapper.insert(address) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
        if (!findAddressById(id)) return Result.opError("没有找到该购物车订单！");
        if (addressMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(Address address) {
        if (!findAddressById(address.getId())) return Result.opError("没有找到该购物车订单！");
        if (addressMapper.updateById(address) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result selectPageList(Integer page, Integer size, String userId, String userName) {
        return null;
    }

    /**
     * 根据Id查询购物车订单是否存在
     */
    public Boolean findAddressById(String id) {
        Address address = addressMapper.selectById(id);
        return address != null;
    }
}
